CMAKE_MINIMUM_REQUIRED(VERSION 3.10)

SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_PROCESSOR cortex-m4)

SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# 设置工具链路径
#SET(TOOLCHAIN_PATH "D:/AppSetup/GNUArmEmbeddedToolchain/10 2021.10/bin")
SET(TOOLCHAIN_PATH "D:/BMSToolChain/GCCARM/bin")

# 指定编译器
SET(CMAKE_C_COMPILER "${TOOLCHAIN_PATH}/arm-none-eabi-gcc.exe")
SET(CMAKE_ASM_COMPILER "${TOOLCHAIN_PATH}/arm-none-eabi-gcc.exe")
SET(CMAKE_CXX_COMPILER "${TOOLCHAIN_PATH}/arm-none-eabi-g++.exe")

# 设置其他工具
SET(CMAKE_OBJCOPY "${TOOLCHAIN_PATH}/arm-none-eabi-objcopy.exe")
SET(CMAKE_SIZE "${TOOLCHAIN_PATH}/arm-none-eabi-size.exe")

# 设置编译选项
SET(CMAKE_C_FLAGS "-mcpu=cortex-m4 -mthumb -g")
SET(CMAKE_ASM_FLAGS "-mcpu=cortex-m4 -mthumb")
SET(CMAKE_CXX_FLAGS "-mcpu=cortex-m4 -mthumb")

# 指定链接脚本
SET(CMAKE_EXE_LINKER_FLAGS "-ffunction-sections -fdata-sections -Wl,--gc-sections,-Map=rt-thread.map,-cref,-u,Reset_Handler -T ${CMAKE_SOURCE_DIR}/linkscripts/STM32F407VE/link.lds -mcpu=cortex-m4 -mthumb")

SET(CMAKE_CXX_STANDARD 14)

PROJECT(bms_build_file C CXX ASM)

SET(USER_INCLUDE_DIRECTORIES
        drivers
        libraries/CMSIS/Device/ST/STM32F4xx/Include
        libraries/CMSIS/Include
        libraries/STM32F4xx_HAL_Driver/Inc
        drivers/include
        drivers/include/config
        dev_rtu
        small_modbus_hmi
        small_modbus_rtu
        small_modbus_tcp
        storage
        applications
)

INCLUDE_DIRECTORIES(
        rt-thread/components/cplusplus
        rt-thread/libcpu/arm/common
        rt-thread/libcpu/arm/cortex-m4
        rt-thread/components/drivers/include
        rt-thread/components/drivers/spi
        rt-thread/components/drivers/spi/sfud/inc
        rt-thread/components/finsh
        .
        rt-thread/include
        rt-thread/components/libc/compilers/common
        rt-thread/components/libc/compilers/newlib
        rt-thread/components/net/lwip-2.0.2/src
        rt-thread/components/net/lwip-2.0.2/src/include
        rt-thread/components/net/lwip-2.0.2/src/include/ipv4
        rt-thread/components/net/lwip-2.0.2/src/arch/include
        rt-thread/components/net/lwip-2.0.2/src/include/netif
        rt-thread/components/net/lwip-2.0.2/src/include/posix
        rt-thread/components/net/netdev/include

        ${USER_INCLUDE_DIRECTORIES}
)

ADD_DEFINITIONS(
        -DHAVE_CCONFIG_H
        -DRT_USING_NEWLIB
        -D_POSIX_C_SOURCE=1
        -D__RTTHREAD__
        -DSTM32F407xx
        -DUSE_HAL_DRIVER
        -DSOC_SERIES_STM32F4
)

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_HAL_Driver/Src USER_HAL_LIST)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/drivers USER_DRI_LIST)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/applications USER_APP_LIST)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/dev_rtu USER_DEV_RTU_LIST)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/small_modbus_hmi USER_SM_MB_LIST)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/small_modbus_rtu USER_SM_MB_RTU_LIST)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/small_modbus_tcp USER_SM_MB_TCP_LIST)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/storage USER_small_STORAGE_LIST)

SET(USER_SOURCES
        ${USER_HAL_LIST}
        ${USER_DRI_LIST}
        ${USER_APP_LIST}
        ${USER_DEV_RTU_LIST}
        ${USER_SM_MB_LIST}
        ${USER_SM_MB_RTU_LIST}
        ${USER_SM_MB_TCP_LIST}
        ${USER_small_STORAGE_LIST}
        #  Add CMSIS Library
        libraries/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c
        libraries/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc/startup_stm32f407xx.S
)

SET(PROJECT_SOURCES
        rt-thread/components/cplusplus/cxx_crt.cpp
        rt-thread/components/cplusplus/cxx_Semaphore.cpp
        rt-thread/components/cplusplus/cxx_Thread.cpp
        rt-thread/components/cplusplus/crt_init.c
        rt-thread/components/cplusplus/cxx_Mutex.cpp
        rt-thread/libcpu/arm/common/backtrace.c
        rt-thread/libcpu/arm/common/div0.c
        rt-thread/libcpu/arm/common/showmem.c
        rt-thread/libcpu/arm/cortex-m4/context_gcc.S
        rt-thread/libcpu/arm/cortex-m4/cpuport.c
        rt-thread/components/drivers/i2c/i2c-bit-ops.c
        rt-thread/components/drivers/i2c/i2c_core.c
        rt-thread/components/drivers/i2c/i2c_dev.c
        rt-thread/components/drivers/misc/adc.c
        rt-thread/components/drivers/misc/dac.c
        rt-thread/components/drivers/misc/pin.c
        rt-thread/components/drivers/serial/serial.c
        rt-thread/components/drivers/spi/sfud/src/sfud.c
        rt-thread/components/drivers/spi/sfud/src/sfud_sfdp.c
        rt-thread/components/drivers/spi/spi_core.c
        rt-thread/components/drivers/spi/spi_dev.c
        rt-thread/components/drivers/spi/spi_flash_sfud.c
        rt-thread/components/drivers/src/completion.c
        rt-thread/components/drivers/src/dataqueue.c
        rt-thread/components/drivers/src/pipe.c
        rt-thread/components/drivers/src/ringblk_buf.c
        rt-thread/components/drivers/src/ringbuffer.c
        rt-thread/components/drivers/src/waitqueue.c
        rt-thread/components/drivers/src/workqueue.c
        rt-thread/components/finsh/shell.c
        rt-thread/components/finsh/msh.c
        rt-thread/components/finsh/cmd.c
        rt-thread/src/clock.c
        rt-thread/src/components.c
        rt-thread/src/device.c
        rt-thread/src/idle.c
        rt-thread/src/ipc.c
        rt-thread/src/irq.c
        rt-thread/src/kservice.c
        rt-thread/src/mem.c
        rt-thread/src/mempool.c
        rt-thread/src/object.c
        rt-thread/src/scheduler.c
        rt-thread/src/signal.c
        rt-thread/src/thread.c
        rt-thread/src/timer.c
        rt-thread/components/libc/compilers/common/time.c
        rt-thread/components/libc/compilers/newlib/libc.c
        rt-thread/components/libc/compilers/newlib/libc_syms.c
        rt-thread/components/libc/compilers/newlib/stdio.c
        rt-thread/components/libc/compilers/newlib/syscalls.c
        rt-thread/components/net/lwip-2.0.2/src/arch/sys_arch.c
        rt-thread/components/net/lwip-2.0.2/src/api/err.c
        rt-thread/components/net/lwip-2.0.2/src/core/ip.c
        rt-thread/components/net/lwip-2.0.2/src/core/tcp_in.c
        rt-thread/components/net/lwip-2.0.2/src/core/stats.c
        rt-thread/components/net/lwip-2.0.2/src/core/tcp.c
        rt-thread/components/net/lwip-2.0.2/src/core/netif.c
        rt-thread/components/net/lwip-2.0.2/src/core/ipv4/dhcp.c
        rt-thread/components/net/lwip-2.0.2/src/api/netdb.c
        rt-thread/components/net/lwip-2.0.2/src/api/tcpip.c
        rt-thread/components/net/lwip-2.0.2/src/core/dns.c
        rt-thread/components/net/lwip-2.0.2/src/core/ipv4/igmp.c
        rt-thread/components/net/lwip-2.0.2/src/core/ipv4/ip4_addr.c
        rt-thread/components/net/lwip-2.0.2/src/apps/ping/ping.c
        rt-thread/components/net/lwip-2.0.2/src/core/def.c
        rt-thread/components/net/lwip-2.0.2/src/core/memp.c
        rt-thread/components/net/lwip-2.0.2/src/core/ipv4/icmp.c
        rt-thread/components/net/lwip-2.0.2/src/core/pbuf.c
        rt-thread/components/net/lwip-2.0.2/src/core/inet_chksum.c
        rt-thread/components/net/lwip-2.0.2/src/core/ipv4/ip4_frag.c
        rt-thread/components/net/lwip-2.0.2/src/core/tcp_out.c
        rt-thread/components/net/lwip-2.0.2/src/core/sys.c
        rt-thread/components/net/lwip-2.0.2/src/netif/ethernetif.c
        rt-thread/components/net/lwip-2.0.2/src/api/api_msg.c
        rt-thread/components/net/lwip-2.0.2/src/api/netifapi.c
        rt-thread/components/net/lwip-2.0.2/src/core/ipv4/autoip.c
        rt-thread/components/net/lwip-2.0.2/src/core/ipv4/ip4.c
        rt-thread/components/net/lwip-2.0.2/src/api/sockets.c
        rt-thread/components/net/lwip-2.0.2/src/netif/lowpan6.c
        rt-thread/components/net/lwip-2.0.2/src/core/ipv4/etharp.c
        rt-thread/components/net/lwip-2.0.2/src/api/api_lib.c
        rt-thread/components/net/lwip-2.0.2/src/core/timeouts.c
        rt-thread/components/net/lwip-2.0.2/src/netif/ethernet.c
        rt-thread/components/net/lwip-2.0.2/src/core/udp.c
        rt-thread/components/net/lwip-2.0.2/src/api/netbuf.c
        rt-thread/components/net/lwip-2.0.2/src/core/init.c
        rt-thread/components/net/lwip-2.0.2/src/core/raw.c
        rt-thread/components/net/netdev/src/netdev_ipaddr.c
        rt-thread/components/net/netdev/src/netdev.c
)

LINK_DIRECTORIES(
)

LINK_LIBRARIES(
        c
        m
)

ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}.elf ${PROJECT_SOURCES} ${USER_SOURCES})

ADD_CUSTOM_COMMAND(TARGET ${CMAKE_PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O binary ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.bin
        COMMAND ${CMAKE_OBJCOPY} -O ihex ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.hex
        COMMAND ${CMAKE_SIZE} --format=berkeley ${CMAKE_PROJECT_NAME}.elf
        COMMENT "Generating binary, hex file, and displaying size"
)
